﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace search
{
	/// <summary>
	/// EditArticle 的摘要说明。
	/// </summary>
	public partial class ActiveArticles : System.Web.UI.Page
	{
		private DataSet dsPostBible;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			HttpCookie nameCookie = Request.Cookies["NickName"];
			if(nameCookie == null)
				Response.Redirect("../client/index.aspx");

//			if(Session["Email"] == null)
//				Response.Redirect("login.aspx");

			if(!Page.IsPostBack)
			{
				ViewState["Sort"] = "";
				ViewState["Order"] = "";
			}
			BindDataGrid_List();
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid_List.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid_List_ItemCommand);
			this.DataGrid_List.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid_List_PageIndexChanged);
			this.DataGrid_List.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid_List_SortCommand);
			this.DataGrid_List.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid_List_ItemDataBound);

		}
		#endregion

		private void BindDataGrid_List()
		{
			dsPostBible = SearchDataAccess.GetPostBible();

			DataSet ds = SearchDataAccess.GetApprovedArticle();

			ds.Tables[0].DefaultView.Sort = ViewState["Sort"].ToString() + ViewState["Order"].ToString();

			this.DataGrid_List.DataSource = ds.Tables[0].DefaultView;
			this.DataGrid_List.DataBind();

			if(ds.Tables[0].Rows.Count <= this.DataGrid_List.PageSize)
			{
				this.DataGrid_List.AllowPaging = false;
				this.Label_Page.Text = "&nbsp;已审核的文章&nbsp;1页，" + ds.Tables[0].Rows.Count.ToString() + " 项";
			}
			else
			{
				this.DataGrid_List.AllowPaging = true;
				this.Label_Page.Text = "&nbsp;已审核的文章&nbsp;" + DataGrid_List.PageCount.ToString() + "页，" + ds.Tables[0].Rows.Count.ToString() + " 项";
			}
		}

		private void DataGrid_List_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if(e.Item.ItemIndex < 0)
				return;

			Button objButton = (Button)e.Item.FindControl("Button_Action");
			DataRowView drv = (DataRowView)e.Item.DataItem;
			string PostID = drv.Row["PostID"].ToString();
			e.Item.Cells[0].Text = "<a href=\"javascript:MaxWindow('Article.aspx?id=" + drv.Row["PostID"] + "&name=" + Server.UrlEncode(Request.Cookies["NickName"].Value) + "','yes');\">" + drv.Row["Subject"].ToString() + "</a>";
			e.Item.Attributes["onmouseover"] = "MO1();";
			e.Item.Attributes["onmouseout"]  = "MU1();";

			e.Item.Cells[2].Text = drv.Row["Topic"].ToString() + "&nbsp;-&nbsp;" + drv.Row["SubTopic"].ToString();
			e.Item.Cells[3].Text = drv.Row["BibleTitle"].ToString() + "&nbsp;" + drv.Row["BibleChapter"].ToString() + "∶" + 
				drv.Row["BibleSection"].ToString() + "&nbsp;-&nbsp;" + drv.Row["BibleChapter_To"].ToString() + "∶" + drv.Row["BibleSection_To"].ToString();

			DataView view = dsPostBible.Tables[0].DefaultView;
			view.RowFilter = "PostID=" + drv.Row["PostID"].ToString();
			for(int i = 0; i < view.Count; i++)
			{
				e.Item.Cells[3].Text += "<br>" + view[i].Row["BibleTitle"].ToString() + "&nbsp;" + view[i].Row["BibleChapter"].ToString() + "∶" + 
					view[i].Row["BibleSection"].ToString() + "&nbsp;-&nbsp;" + view[i].Row["BibleChapter_To"].ToString() + "∶" + view[i].Row["BibleSection_To"].ToString();
			}
		}

		private void DataGrid_List_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{

			if(e.CommandName == "Delete")
			{
				Label objLabel = (Label)e.Item.FindControl("Label_ID");
				SearchDataAccess.DeleteArticle(objLabel.Text);
				ShowArrow();
			}
		}

		private void DataGrid_List_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			if(ViewState["Sort"].ToString() == e.SortExpression)
			{
				if(ViewState["Order"].ToString() == "")
					ViewState["Order"] = " DESC";
				else
					ViewState["Order"] = "";
			}
			else
			{
				ViewState["Sort"] = e.SortExpression;
				ViewState["Order"] = "";
			}
			
			ShowArrow();
		}


		private void ShowArrow()
		{
			for(int i = 0; i < this.DataGrid_List.Columns.Count; i++)
			{
				string text = this.DataGrid_List.Columns[i].HeaderText;
				this.DataGrid_List.Columns[i].HeaderText = this.DataGrid_List.Columns[i].HeaderText.Replace("↑","");
				this.DataGrid_List.Columns[i].HeaderText = this.DataGrid_List.Columns[i].HeaderText.Replace("↓","");

				if(ViewState["Sort"].ToString() == this.DataGrid_List.Columns[i].SortExpression)
				{
					if(ViewState["Order"].ToString() == "")
					{
						this.DataGrid_List.Columns[i].HeaderText += "↑";
					}
					else
					{
						this.DataGrid_List.Columns[i].HeaderText += "↓";
					}
				}
			}
			BindDataGrid_List();
		}

		private void DataGrid_List_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			this.DataGrid_List.CurrentPageIndex = e.NewPageIndex;
			ShowArrow();
		}



	}
}
